package org.jeecg.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.Md5Util;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.app.entity.AppFeedback;
import org.jeecg.modules.app.req.FeedbackReq;
import org.jeecg.modules.app.service.IAppFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 问题反馈
 * @Author: sean
 * @Date: 2024-09-28
 * @Version: V1.0
 */
@Api(tags = "问题反馈")
@RestController
@RequestMapping("/app/feedback")
@Slf4j
public class AppFeedbackController extends JeecgController<AppFeedback, IAppFeedbackService> {
    @Autowired
    private IAppFeedbackService appFeedbackService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 分页列表查询
     *
     * @param appFeedback
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "问题反馈-分页列表查询")
    @ApiOperation(value = "问题反馈-分页列表查询", notes = "问题反馈-分页列表查询")
    @GetMapping(value = "/list")
    @PermissionData(pageComponent="app/feedback/list")
    public Result<IPage<AppFeedback>> queryPageList(AppFeedback appFeedback,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        QueryWrapper<AppFeedback> queryWrapper = QueryGenerator.initQueryWrapper(appFeedback, req.getParameterMap());
        Page<AppFeedback> page = new Page<AppFeedback>(pageNo, pageSize);
        IPage<AppFeedback> pageList = appFeedbackService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param appFeedback
     * @return
     */
    @AutoLog(value = "问题反馈-添加")
    @ApiOperation(value = "问题反馈-添加", notes = "问题反馈-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AppFeedback appFeedback) {
        appFeedbackService.save(appFeedback);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param appFeedback
     * @return
     */
    @AutoLog(value = "问题反馈-编辑")
    @ApiOperation(value = "问题反馈-编辑", notes = "问题反馈-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AppFeedback appFeedback) {
        appFeedbackService.updateById(appFeedback);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "问题反馈-通过id删除")
    @ApiOperation(value = "问题反馈-通过id删除", notes = "问题反馈-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        appFeedbackService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "问题反馈-批量删除")
    @ApiOperation(value = "问题反馈-批量删除", notes = "问题反馈-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.appFeedbackService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "问题反馈-通过id查询")
    @ApiOperation(value = "问题反馈-通过id查询", notes = "问题反馈-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AppFeedback> queryById(@RequestParam(name = "id", required = true) String id) {
        AppFeedback appFeedback = appFeedbackService.getById(id);
        if (appFeedback == null) {
            return Result.error("未找到对应数据", null);
        }
        return Result.OK(appFeedback);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param appFeedback
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AppFeedback appFeedback) {
        return super.exportXls(request, appFeedback, AppFeedback.class, "问题反馈");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AppFeedback.class);
    }

    @AutoLog(value = "故障质询--技术支持")
    @ApiOperation(value = "故障质询--技术支持", notes = "故障质询--技术支持")
    @PostMapping(value = "/advice")
    public Result<String> advice(MultipartRequest files, @Validated FeedbackReq feedbackReq) {
        //验证码校验
		String captcha = feedbackReq.getCaptcha().toLowerCase();;
		String checkKey = feedbackReq.getCheckKey();
        String realKey = Md5Util.md5Encode(captcha + checkKey, "utf-8");
        Object checkCode = redisUtil.get(realKey);
        if (checkCode == null || !checkCode.toString().equals(captcha)) {
            log.error("验证码错误，key= {} , Ui checkCode= {}, Redis checkCode = {}", checkKey, captcha, checkCode);
            return Result.error("验证码错误");
        }
        appFeedbackService.advice(feedbackReq, files);
        return Result.OK("反馈成功！");
    }

}
